package com.gaowl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gaowl.gulimall.product.entity.BrandEntity;
import com.gaowl.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gaowl.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gaowl.gulimall.product.service.CategoryBrandRelationService;
import com.gaowl.common.utils.PageUtils;
import com.gaowl.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author gaowl
 * @email gaowl@stu.jiangnan.edu.cn
 * @date 2021-03-11 13:20:43
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;


    /**
     * 1、Controller：处理网络请求，接受和校验数据
     * 2、Service接受Controller传来的数据，进行业务处理
     * 3、Controller接受service返回的处理结果，封装成前端页面所需的vo进行返回
     */


    /**
     * 获取分类关联的品牌
     */
    @RequestMapping("/brands/list")
    public R getAssociatedBrands(@RequestParam(value = "catId", required = true) Long catId){
        // Tips：不直接返回BrandVo，以便后面可能复用service里的方法！
        List<BrandEntity> brandEntityList = categoryBrandRelationService.findAssociatedBrandsByCatId(catId);

        List<BrandVo> brandVoList = brandEntityList.stream().map((brandEntity) -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", brandVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据品牌id查出关联所有信息
     * @param brandId 品牌的id
     */
    @GetMapping("catelog/list")
    public R catelogList(@RequestParam Long brandId){
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);

        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(queryWrapper);
        return R.ok().put("data", data);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		//categoryBrandRelationService.save(categoryBrandRelation);

        // 传入品牌id和分类id，需查询出品牌名称、分类名称等其他信息再保存
        categoryBrandRelationService.saveDetail(categoryBrandRelation);  // 保存详细的管理信息

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
